<template>
<div class="register">
    <div class="header">
      <h1>欢迎进行注册</h1>
    </div>
    <el-form :model="registerForm" :rules="rules" ref="registerForm" label-width="100px" class="registerForm">
        <el-form-item label="用户昵称" prop="userName">
            <el-input v-model="registerForm.userName"  placeholder="起个好听的名字方便大家找到你"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
            <el-input v-model="registerForm.email"  placeholder="用户登录与找回密码用且不可修改，目前仅支持QQ邮箱，请谨慎填写"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
            <el-input v-model="registerForm.phone"  placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="出生日期" required>
            <el-col :span="11">
            <el-form-item prop="bornTime">
                <el-date-picker type="date" placeholder="选择日期" @change="updateDate" v-model="registerForm.bornTime" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
            </el-col>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="registerForm.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="registerForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="registerForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button ref="submit" type="primary" @click="submitForm()">立即注册</el-button>
            <el-button @click="resetForm('registerForm')">返回登录</el-button>
        </el-form-item>
    </el-form>
</div>  
</template>

<script>
export default {
 data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.registerForm.checkPass !== '') {
            this.$refs.registerForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.registerForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
            callback();
        }
      };
      return {
        registerForm: {
          userName: '',
          emali: '',
          bornTime: '',
          sex: '',
          password:'',
          checkPass:'',
          phone:'', 
        },
        rules: {
          name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          email :[
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
           ],
          date: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
          sex: [
            { required: true, message: '请选择活动资源', trigger: 'change' }
          ],
          pass: [
            { validator: validatePass, trigger: 'blur' },
            { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' },
            { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm() {
        console.log(this.bornTime);
        this.registerUser();
      },
      resetForm(registerForm) {
        this.$refs[registerForm].resetFields();
        this.$router.push('/login');

      },
      updateDate(val){
        this.bornTime = val;
      },
      registerUser(){
         this.$refs.registerForm.validate(async valid => {
        if(!valid) return;
        if (''!=this.bornTime&&''!=this.phone&&''!=this.password&&''!=this.email&&''!=this.userName) {
          const {data:res} = await this.$axios.post("/user/registerUser.do",this.registerForm)//访问后台s
          console.log('返回值：'+res);
          if(res=='ok'){
            this.$message.success("注册成功！即将返回登录！");
            this.$router.push({path:"/login"});
          }
          if(res=='errorEmail'){
            this.$message.error("注册失败!邮箱已经被注册。");
            return false;  
          }
          if(res=='errorPhone'){
            this.$message.error("注册失败！手机号已经被注册。");
            return false;  
          }

          if(res=='error')  {
            this.$message.error("注册失败！请检查相关个人信息是否正确填写,如qq邮箱是否按照正确格式填写，手机号是否正确");
            return false;
          }
          // window.sessionStorage.setItem("userName",res.userName);
          // window.sessionStorage.setItem("flag",res.flag);
        } else {
          this.$message.error("注册失败！请完善相关个人信息。");
          return false;
        }
      });
      },
    }
}
</script>

<style lang="less" scoped>
.register{
  position: relative;
  height: 100%;
  width: 100%;
  .registerForm{
      position: absolute;
      width: 60%;
      height: 100%;
      margin-top: 3rem;
      top: 15%;
      transform: translateX(-50%);
      left: 50%;
  }
}
.header{
  background-color: rgb(14, 156, 238);
  height: 15%;
  width: 100%;
  position: absolute;
  top: 0%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
//   justify-content: space-between;
}
</style>